package com.train.forum.utils;

import lombok.Data;

@Data
public class R<T> {

    public static final Integer DEFAULT_SUCCESS_CODE=200;
    public static final String DEFAULT_SUCCESS_MSG="请求成功";
    public static final Integer DEFAULT_ERROR_CODE=500;
    public static final String DEFAULT_ERROR_MSG="请求失败";
    private int code;
    private String msg;
    private T data;
    public R setSuccess(boolean success) {
        return success ? setCode(DEFAULT_SUCCESS_CODE).setMsg(DEFAULT_SUCCESS_MSG)
                : setCode(DEFAULT_ERROR_CODE).setMsg(DEFAULT_ERROR_MSG);
    }
    public R setCode(int code) {
        this.code=code;
        return this;
    }
    public R setMsg(String msg) {
        this.msg=msg;
        return this;
    }
    public R setData(T data) {
        this.data=data;
        return this;
    }

    public R(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R(T data) {
        this.code=DEFAULT_SUCCESS_CODE;
        this.msg=DEFAULT_SUCCESS_MSG;
        this.data = data;
    }

    public R() {
    }
}
